<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Message Extends MY_parent
{
	private $students = [];
	private $student_chooser = TRUE;
	private $_user_id = '';

	public function __construct()
	{
		parent::__construct();
		if($this->session->userdata('logged_in') == FALSE)
		{
			redirect();
		}else{
			$this->load->model('M_message','me');
			$this->view_data['system_message'] =$this->_msg();
		}
		$this->view_data['tiny_mce'] = '<script type="text/javascript" src="'.base_url('assets/tinymce/tinymce.min.js').'"></script>';

		$this->page_auth->secure_page();
		$this->view_data['system_message'] = $this->_msg();
		$this->_user_id = $this->session->userdata('userid');
		$this->load->model('M_parent');
		$this->load->library('hs');
		$this->_get_students_ids();
		$this->_student_chooser();

	}
	
	public function process()
	{
		if(IS_AJAX)
		{
			$post = $this->input->post();
			pp($post);
			exit();
		}else{

			$post = $this->input->post();
			if($this->input->post('send_message'))
			{
				
				$result = $this->me->process_message($post);
				
				if($result)
				{
					$this->_msg('s','Message Was Sent','message');
				}else{
					$this->_msg('e','Unable to send message','message');
				}
			}elseif($this->input->post('reply_to_thread'))
			{
				$result = $this->me->reply_to_thread($post);

				if($result)
				{
					$this->_msg('s','Message Was Sent','message/thread/'.$post['hash']);
				}else{
					$this->_msg('e','Unable to send message','message');
				}
			}
		}
	}

	public function index()
	{
		$user_id = $this->session->userdata('userid');
		
		// $this->view_data['employees'] = $this->me->get_all_employees();
		
		if(strtolower($this->session->userdata('userType')) == 'guardian'){
			$this->view_data['advisers'] = $this->_get_student_advisers();
		}else{
			$this->view_data['advisers'] = $this->_get_student_advisers($this->session->userdata('id_e'));
		}
		$this->view_data['gradelevels'] = $this->me->get_all_levels();
		$this->view_data['sections'] = $this->me->get_all_sections();
		$this->view_data['myinbox'] = $this->me->inbox();
		$this->view_data['delbox'] = $this->me->delbox();
		$this->view_data['count_viewer'] = function($thread_id){return $this->me->count_viewer($thread_id);};
	}

	public function remove_me($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$result = $this->me->remove_me_also($thread_id);
			if($result)
			{
				$this->_msg('s','Successfully removed from the thread.','message');
			}else{
				$this->_msg('e','Unable to remove you from the thread.','message');
			}
		}else{
			$this->_msg('n','Invalid URL supplied.','message');
		}
	}

	public function thread($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$this->me->set_read($thread_id);
			$this->view_data['thread'] = $this->me->show_thread($thread_id);
			$this->view_data['thread_id'] = $thread_id;
			$this->view_data['hash'] = $hash;
			
		}else{
			$this->_msg('n','Invalid URL Supplied','message');
		}
	}
	
	public function delete($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$result = $this->me->remove_me($thread_id);
			if($result)
			{
				$this->_msg('s','Thread deleted.','message');
			}else{
				$this->_msg('e','Unable to delete thread.','message');
			}
		}else{
			$this->_msg('n','Invalid URL supplied.','message');
		}
	}
	
	public function del_permanently($hash)
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$result = $this->me->remove_me_permanently($thread_id);
			if($result)
			{
				$this->_msg('s','Permanently deleted.','message');
			}else{
				$this->_msg('e','Unable to delete thread.','message');
			}
		}else{
			$this->_msg('n','Invalid URL supplied.','message');
		}
	}
	
	/** Get Students Advisers
	 * Loop each student and get their respective advisers
	 * @return - OBJECT
	 */
	private function _get_student_advisers($e_id = false){

		$this->load->model('M_sections');

		if($e_id){

			$section_data = $this->M_sections->get_section_data($e_id);
			
			$adv = false;
			
			if($section_data){
				$adv[$section_data['data']->emp_id] = $section_data['data']->fullname;
			}

			$return['Select from the ff: advisers'] = $adv;

			return $return;

		}else{
			if($this->students){
			
				$adv = false;
				
				foreach ($this->students as $key => $st) {
					
					$sqlp = 'SELECT e_id FROM enrollments WHERE e_profile_id = ? ORDER BY e_id DESC LIMIT 1';
					$resp = $this->db->query($sqlp,[$st->p_id]);
					
					if($resp->num_rows() >=1){
					$section_data = $this->M_sections->get_section_data($resp->row()->e_id);
					
					if($section_data){

						$adv[$section_data['data']->emp_id] = $section_data['data']->fullname;

					}
					}
				}

				$return['Select from the ff: advisers'] = $adv;

				return $return;
			}
		}

		return false;
	}

	private function _get_students_ids()
	{
		$userid = $this->session->userdata('userid');
		$this->students = $this->M_parent->get_user_students($userid);
	}

	private function _student_chooser()
	{
		if($this->student_chooser)
		{
			$this->view_data['for_student_chooser']['raw'] = $this->students;
			$this->view_data['for_student_chooser']['student'] = $this->M_parent->students($this->_user_id);
		}
	}
}